home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Periodic Tasksƒ / CPPWatchWriteTask.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  2.8 KB  |  90 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/17/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPWatchWriteTasks
  6.     
  7.     SUPERCLASS: CPeriodicTask
  8.     
  9.         This C++ class waits until there are no write tasks in the 
  10.         queue it belongs to and then disposes of the memory it is
  11.         started up with.  
  12.         Q:  What could you possibly use this for?
  13.         A:  When you spawn a write task, it has to be assured that the
  14.             data it is writing out won't get cleared.  Normally you could
  15.             tell it that it has ownership of the data and let it dispose
  16.             of it when it is done.  However, if you spawn many write tasks,
  17.             all with the same data, you have no guarantee the one you gave 
  18.             ownership of the data to will complete last, or, indeed, at all.
  19.             To solve this, do not give any of the write tasks ownership of
  20.             the data, then spawn a CWatchWriteMemTask and give it the
  21.             responsibility of disposing of the memory after all of the write
  22.             tasks have completed.
  23.     
  24. ********************************************************************/
  25.  
  26. #include "CPPWatchWriteTask.h"
  27. #include <CPPTaskManager.h>
  28. #include <MemoryTools.h>
  29.  
  30. /*-----------------------------------------------------------------*/
  31. /*------------------------ PUBLIC METHODS -------------------------*/
  32. /*-----------------------------------------------------------------*/
  33.  
  34.     CPPWatchWriteTasks::CPPWatchWriteTasks (CPPTaskManager *TaskManager, 
  35.                                             long minPeriod) :
  36.                         CPPPeriodicTask (TaskManager, minPeriod, TRUE)
  37.     {
  38.         this->dataToWatch = NULL;
  39.     }
  40.  
  41. /*-----------------------------------------------------------------*/
  42.  
  43.     CPPWatchWriteTasks::~CPPWatchWriteTasks (void)
  44.     {
  45.     }
  46.             
  47. /*-----------------------------------------------------------------*/
  48.  
  49.     char *CPPWatchWriteTasks::ClassName (void)
  50.     {
  51.         return "CPPWatchWriteTasks";
  52.     }
  53.  
  54. /*-----------------------------------------------------------------*/
  55.  
  56.     void CPPWatchWriteTasks::StartWatchTask (Ptr Data)
  57.     {
  58.         if (!this->hasCompleted)
  59.           return;
  60.         
  61.         // save the pointer we are responsible for
  62.         this->dataToWatch = Data;
  63.  
  64.         // now add the task to the periodic task manager queue
  65.         this->hasCompleted = FALSE;
  66.         this->ourManager->AddPeriodicTask(this);
  67.     }
  68.     
  69. /*-----------------------------------------------------------------*/
  70.  
  71.     void CPPWatchWriteTasks::DoPeriodicAction (void)
  72.     // Call our manager's "HowManyTasksOfType" routine to count */
  73.     /* the number of 'write' tasks in the queue; if there aren't */
  74.     /* any, we should signal completion */
  75.     {
  76.         // call the inherited method to update frequency count
  77.         CPPPeriodicTask::DoPeriodicAction();
  78.  
  79.         if (this->ourManager->HowManyTasksOfType("CPPWriteTask") <= 0)
  80.           this->hasCompleted = TRUE;
  81.     }
  82.  
  83. /*-----------------------------------------------------------------*/
  84.  
  85.     void CPPWatchWriteTasks::DoCompletedAction (void)
  86.     {
  87.         CPPPeriodicTask::DoCompletedAction();
  88.         NukePtr(this->dataToWatch);
  89.     }
  90.